package BNS.plumbing;

import java.awt.Point;

import javax.swing.JFrame;


/** A class that is useful during debugging. It supports the creation of a fully-functioning
	Client without the fuss and overhead of starting multiple browser windows.
	TODO: fix bugs in here: the LocalClients are not cleaned up properly.
	*/
public class LocalClient extends JFrame implements Runnable {
	
	/**
   * 
   */
  private static final long serialVersionUID = 1L;
  private BNSPersona persona;
	private ClientLiaison liaison;
	private static int count=0;
	
	/** Creates a new LocalClient and positions it on the screen in one of 6 preordained spots.
		This creats a very orderly display where all Clients can be seen at once, but the size
		of each panel is ignorant of the GUI elements so may not actually fit.
		*/
	public LocalClient(ClientLiaison liais, BNSPersona pers) {
		super();
		liaison= liais;
		persona= pers;
		setTitle("LocalClient");
		setSize(450,270);
		
		System.err.println("got BNSPersona "+ persona.getTitle());				
		getContentPane().add(persona.startPanel());
		validate();		// *parent* of persona must lay out all components
		persona.start();
		
		int x= ((count/3)%2) * 450;
		int y= 23+(count%3) * 260;
		setLocation(new Point(x,y));
		count++;
		
		setVisible(true);
		repaint();
		Thread dostuff= new Thread(this);
		dostuff.start();
		}
	
	/** performs the same function as its namesake in Client. */
	public void run() {
		try {
			while(true) {
				java.io.Serializable blob= liaison.passUpdate();
				if (blob==null || blob instanceof String || blob instanceof TextPersona) 
					continue;
				//System.err.println("blob="+blob);
				persona.gameUpdate(blob);
				Thread.sleep(300);
				}	
			}
		catch(java.rmi.RemoteException e) {}
		catch(java.lang.InterruptedException e) {}
		}
	
	/** stops and disposes of the panel used to display this LocalClient. */
	public void shutdown() {
		persona.stop();
		setVisible(false);
		dispose();
		}
	
	} // EOClass PlayerPanel
